package com.codeavatar.hardwaretester.activity.hardware;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.codeavatar.catools.base.CaBaseActivity;
import com.codeavatar.catools.helper.CaActionBarHelper;
import com.codeavatar.catools.helper.CaIntentHelper;
import com.codeavatar.hardwaretester.R;
import com.codeavatar.hardwaretester.activity.hardware.bluetooth.AiBoxActivity;
import com.codeavatar.hardwaretester.activity.hardware.bluetooth.CarBleActivity;
import com.codeavatar.hardwaretester.activity.hardware.bluetooth.ServerActivity;
import com.codeavatar.hardwaretester.adapter.RvwMenuListAdapter;
import com.codeavatar.hardwaretester.data.MenuData;
import com.codeavatar.hardwaretester.databinding.ActivityBluetoothHomeBinding;
import com.codeavatar.hardwaretester.model.BluetoothConnectModel;
import com.codeavatar.hardwaretester.model.MenuModel;
import com.codeavatar.hardwaretester.service.BluetoothBleService;

import java.util.ArrayList;
import java.util.List;

/**
 * +----------------------------------------------------------------------
 * | @Author: codeavatar   @Year：2022
 * +----------------------------------------------------------------------
 * | @Email: codeavatar@aliyun.com
 * +----------------------------------------------------------------------
 **/
public class BluetoothActivity extends CaBaseActivity {

    private final String TAG = this.getClass().getSimpleName();

    private ActivityBluetoothHomeBinding homeBinding;
    private RvwMenuListAdapter rvwMenuListAdapter;
    private List<MenuModel> menuModelList = new ArrayList<>();

    //++++++++++++++++++++++++++++++++++++++
    //++ 内部方法
    //++++++++++++++++++++++++++++++++++++++

    //++++++++++++++++++++++++++++++++++++++
    //++ 内部方法
    //++++++++++++++++++++++++++++++++++++++

    private void doInitBleInfo(){
        BluetoothBleService service = BluetoothBleService.init();
        if(null != service){
            if(service.isConnecting()){
                homeBinding.tvwBleInfo.setText("蓝牙状态：连接中");
                BluetoothConnectModel model = BluetoothBleService.init().getBluetoothConnectModel();
                homeBinding.tvwBleDeviceId.setText("设备ID："+model.getBluetoothDevice().getAddress());
                homeBinding.tvwBleServiceId.setText("服务UUID："+model.getBluetoothGattService().getUuid().toString());
                homeBinding.tvwBleCharacteristicId.setText("特征UUID："+model.getBluetoothGattCharacteristic().getUuid().toString());
            }else{
                homeBinding.tvwBleInfo.setText("蓝牙状态：未连接");
                homeBinding.tvwBleDeviceId.setText("设备ID：--");
                homeBinding.tvwBleServiceId.setText("服务UUID：--");
                homeBinding.tvwBleCharacteristicId.setText("特征UUID：--");
            }
        }
    }

    private void doInit(){
        rvwMenuListAdapter = new RvwMenuListAdapter(this, new RvwMenuListAdapter.IClickCallback() {
            @Override
            public void onClick(MenuModel menu, int position) {
                if("car_ble".equals(menu.getAction())){
                    toStart(CarBleActivity.class,null);
                }else if("ai_box".equals(menu.getAction())){
                    toStart(AiBoxActivity.class,null);
                }
            }

            @Override
            public void onChildClick(View v, int position) {

            }
        });
        menuModelList = MenuData.Init().getBluetoothMenuList();
        rvwMenuListAdapter.setDataList(this.menuModelList);
        homeBinding.rvwList.setLayoutManager(new LinearLayoutManager(this));
        homeBinding.rvwList.setAdapter(this.rvwMenuListAdapter);
    }

    //++++++++++++++++++++++++++++++++++++++
    //++ 重写方法
    //++++++++++++++++++++++++++++++++++++++

    @Override
    protected void initBinding() {
        homeBinding = ActivityBluetoothHomeBinding.inflate(getLayoutInflater());
    }

    @Override
    protected View getLayoutView() {
        return homeBinding.getRoot();
    }

    @Override
    protected void initNavigation() {
        CaActionBarHelper.init().toolbar(getSupportActionBar(),"蓝牙");
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {
        this.doInit();
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.bluetooth_home_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int itemId = item.getItemId();
        if(android.R.id.home == itemId){
            this.finish();
        }else if(R.id.action_start_server == itemId){
            CaIntentHelper.init().startActivity(gActivity, ServerActivity.class);
        }else{
            return super.onOptionsItemSelected(item);
        }
        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        this.doInitBleInfo();
    }
}
